package com.somlsis.activity

import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import com.base.AppConfig
import com.base.BaseActivity
import com.base.DeviceCenter
import com.clj.fastble.data.BleDevice
import com.event.BindingDeviceEvent
import com.net.HttpResponse
import com.somlsis.R
import com.somlsis.bean.Device
import com.somlsis.bean.DeviceInfo
import com.somlsis.databinding.ActivityAddDeviceBinding
import com.somlsis.fragment.BindingDeviceFragment
import com.somlsis.fragment.SearchDeviceFragment
import org.greenrobot.eventbus.EventBus
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class AddDeviceActivity : BaseActivity<ActivityAddDeviceBinding>() {
    private lateinit var searchDeviceFragment: SearchDeviceFragment
    private lateinit var bindingDeviceFragment: BindingDeviceFragment
    private var fragments = ArrayList<Fragment>()
    private var oldIndex = -1
    private var mBleDevice: BleDevice? = null
    override fun getViewBinding(): ActivityAddDeviceBinding {
        return ActivityAddDeviceBinding.inflate(LayoutInflater.from(this))
    }

    override fun createView() {
        binding.layoutTitle.back.setOnClickListener {
            finish()
        }
        searchDeviceFragment = SearchDeviceFragment(object :
            OnAddDeviceNextClickListener {
            override fun onSearchDeviceConnectClick(bleDevice: BleDevice) {
                super.onSearchDeviceConnectClick(bleDevice)
                showFragment(1)
                mBleDevice = bleDevice

                bindingDevice(bleDevice)
            }


        })
        bindingDeviceFragment = BindingDeviceFragment(object :
            OnAddDeviceNextClickListener {
            override fun onBindingDeviceCallBack() {
                super.onBindingDeviceCallBack()
                mBleDevice?.let {
                    setResult(AppConfig.RESULT_OK, intent.putExtra("device_mac", it.mac))
                    finish()
                }
            }
        })
        fragments.add(searchDeviceFragment)
        fragments.add(bindingDeviceFragment)
        addFragment()
    }


    interface OnAddDeviceNextClickListener {
        fun onSearchDeviceConnectClick(bleDevice: BleDevice) {}
        fun onBindingDeviceCallBack() {}
    }

    private fun addFragment() {
        supportFragmentManager.beginTransaction()
            .add(R.id.add_device_fl, fragments[0], "0").show(fragments[0])
            .commit() // add下一个到Activity中
        supportFragmentManager.beginTransaction()
            .add(R.id.add_device_fl, fragments[1], "1").hide(fragments[1])
            .commit() // add下一个到Activity中
    }

    private fun showFragment(index: Int) {
        if (oldIndex == -1) {
            supportFragmentManager.beginTransaction()
                .show(fragments[index]).commit() // 显示下一个
        } else {
            supportFragmentManager.beginTransaction()
                .hide(fragments[oldIndex]).show(fragments[index])
                .commit() // 隐藏当前的fragment，显示下一个
        }
        oldIndex = index
    }

    fun bindingDevice(bleDevice: BleDevice) {
        deviceCenter.getDeviceCenterInfo(bleDevice, object : DeviceCenter.onDeviceInfoCallBack {
            override fun deviceInfoCallBack(deviceInfo: DeviceInfo) {
                dataCenter.bindingDevice(deviceInfo, object : Callback<HttpResponse<String>> {
                    override fun onResponse(
                        call: Call<HttpResponse<String>>,
                        response: Response<HttpResponse<String>>
                    ) {
                        EventBus.getDefault().post(BindingDeviceEvent())
                        bindingDeviceFragment.bindingSuccess()
                    }

                    override fun onFailure(call: Call<HttpResponse<String>>, t: Throwable) {
                    }

                })
            }

        })
    }
}